<?php

namespace App\Rules\Work;

use App\Enums\WorkOrder\OrderEnum;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\WorkOrder\WorkOrderApplyContentModel;
use App\Models\WorkOrder\WorkOrderApplyMainModel;
use Illuminate\Contracts\Validation\Rule;

class CheckCertificates implements Rule
{

    public string $msg = '';

    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        //$value 功能开通表单申请ID


        //判断对公对私 collectionType

        $applyContentModel = WorkOrderApplyContentModel::query()
            ->with(['applyMainRel'])
            ->find($value);

        if (null == $applyContentModel) {
            return true; // 不做校验
        }
        if (null == $applyContentModel->applyMainRel) {
            return true;
        }

        //二证合一仅在夸父收银中判断
        if ($applyContentModel->applyMainRel->problemChildId != OrderEnum::PROBLEM_PID_5_CHILDREN_100 || $applyContentModel->applyMainRel->problemPid != OrderEnum::PROBLEM_PID_5) {
            return true;
        }

        $content = json_decode($applyContentModel->content, true) ?: [];
        $collectionType = $content['collectionType'] ?? '';
        $shopId = $applyContentModel->applyMainRel->shopId;
        if (!mb_strlen($collectionType)) {
            return true;
        }

        switch ($collectionType) {
            case WorkOrderApplyContentModel::COLLECTION_TYPE_PUBLIC:
                //对公不做判断

                //夸父授权书-申请人-公司名称、营业执照-公司名称，需要保持一致
//                $mainProcessModel = OpenShopMainProcessModel::query()
//                    ->where('shopId', $shopId)
//                    ->with(['userContractRel'])
//                    ->first();
//                if (null == $mainProcessModel) {
//                    return true;
//                }
//                if (null == $mainProcessModel->userContractRel) {
//                    return true;
//                }
//                $empowerBusinessName       = $mainProcessModel->userContractRel->companyName;
//                $empowerBusinessNumber     = $mainProcessModel->userContractRel->businessLicenseNumber;
//                if (mb_strlen($content['enterpriseName'] ?? '')) {
//                    if ($empowerBusinessName != $content['enterpriseName']) {
//                        return false;
//                    }
//                }
                break;
            case WorkOrderApplyContentModel::COLLECTION_TYPE_PRIVATE:
                //夸父授权书-申请人、身份证
                $mainProcessModel = OpenShopMainProcessModel::query()
                    ->where('shopId', $shopId)
                    ->with(['userIdentityCardRel'])
                    ->orderBy('id', 'desc')
                    ->first();
                if (null == $mainProcessModel) {
                    return true;
                }
                if (null == $mainProcessModel->userIdentityCardRel) {
                    return true;
                }
                $empowerUserName       = $mainProcessModel->userIdentityCardRel->name;
                $empowerUserCardNumber = $mainProcessModel->userIdentityCardRel->identityCardNumber;
                //collectionPersonUsername   collectionPersonIdentityCardNumber  收款人  legalPersonUsername  法人姓名
                if (mb_strlen($content['collectionPersonUsername'] ?? '')) {
                    if ($empowerUserName != $content['collectionPersonUsername']) {
                        $this->msg = '夸父授权书申请人与收款人信息不一致，请修改后提交';
                        return false;
                    }
                }
                if (mb_strlen($content['legalPersonUsername'] ?? '')) {
                    if ($empowerUserName != $content['legalPersonUsername']) {
                        $this->msg = '夸父授权书申请人与法人姓名信息不一致，请修改后提交';
                        return false;
                    }
                }
                if (mb_strlen($content['collectionPersonIdentityCardNumber'] ?? '')) {
                    if ($empowerUserCardNumber != $content['collectionPersonIdentityCardNumber']) {
                        $this->msg = '夸父授权书申请人身份证号与收款人身份证号信息不一致，请修改后提交';
                        return false;
                    }
                }
                break;
            default:
                return true;
        }
        return true;
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return $this->msg;
    }
}
